import * as Api from "@req/api";

export const suc = (str) => {
  if (str != "success") {
    return false;
  }
  return true;
};

export const getDesc = (str) => {
  if (str == null) {
    return "";
  }
  return str.replace(/<[^>]+>/g, "");
};

export const baseURl = () => {
  let BASE_URL = import.meta.env.VITE_SERVER_LOC_HOST; // 测试

  // if (window.location.href.indexOf("fancy-metal") != -1) {
  //   BASE_URL = import.meta.env.VITE_SERVER_DEV_HOST; // 测试
  // }

  if (window.location.href.indexOf("fancy-metal") != -1) {
    BASE_URL = import.meta.env.VITE_SERVER_PROD_HOST; // 测试
  }
  return BASE_URL;
};

export const upImgFun = async (file) => {
  const list = ["image/jpeg", "image/png"];
  if (!list.includes(file.file.type)) {
    message.error("请上传图片格式!");
    return;
  }
  let forms = new FormData();
  forms.append("file", file.file);
  return await Api.uploadFile(forms).then((res) => {
    if (suc(res.status)) {
      return import.meta.env.VITE_SERVER_PROD_HOST + res.filepath;
    }
  });
};

export const toobarCfg = () => {
  return {
    toolbarKeys: [
      "blockquote",
      // 分割线
      "|",
      // 菜单 key
      "bold",
      "underline",
      "italic",
      {
        key: "group-more-style", // 必填，要以 group 开头
        title: "更多", // 必填
        iconSvg:
          '<svg viewBox="0 0 1024 1024"><path d="M204.8 505.6m-76.8 0a76.8 76.8 0 1 0 153.6 0 76.8 76.8 0 1 0-153.6 0Z"></path><path d="M505.6 505.6m-76.8 0a76.8 76.8 0 1 0 153.6 0 76.8 76.8 0 1 0-153.6 0Z"></path><path d="M806.4 505.6m-76.8 0a76.8 76.8 0 1 0 153.6 0 76.8 76.8 0 1 0-153.6 0Z"></path></svg>', // 可选
        menuKeys: ["through", "code", "sup", "sub", "clearStyle"], // 下级菜单 key ，必填
      },
      "color",
      "bgColor",
      "|",
      "fontSize",
      "fontFamily",
      "lineHeight",
      "|",
      "bulletedList",
      "numberedList",
      "todo",
      {
        key: "group-justify",
        title: "对齐",
        iconSvg:
          '<svg viewBox="0 0 1024 1024"><path d="M768 793.6v102.4H51.2v-102.4h716.8z m204.8-230.4v102.4H51.2v-102.4h921.6z m-204.8-230.4v102.4H51.2v-102.4h716.8zM972.8 102.4v102.4H51.2V102.4h921.6z"></path></svg>',
        menuKeys: [
          "justifyLeft",
          "justifyRight",
          "justifyCenter",
          "justifyJustify",
        ],
      },
      {
        key: "group-indent",
        title: "缩进",
        iconSvg:
          '<svg viewBox="0 0 1024 1024"><path d="M0 64h1024v128H0z m384 192h640v128H384z m0 192h640v128H384z m0 192h640v128H384zM0 832h1024v128H0z m0-128V320l256 192z"></path></svg>',
        menuKeys: ["indent", "delIndent"],
      },
      "|",
      "insertLink",
      "uploadImage",
      "divider",
      "|",
      "fullScreen",
    ],
  };
};

export const findObjItem = (objList, key, value) => {
  if (!value || !objList) return;
  for (const item of objList) {
    if (item[key] === value) {
      return item;
    } else if (item.children) {
      const res = findObjItem(item.children, key, value);
      if (res) return res;
    }
  }
  return null;
};
